package com.javademo.multithreadingtest;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InterruptTest {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new MyThreadTest1();
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
        thread.join();
        log.info("thread is over");
    }
}

@Slf4j
class MyThreadTest1 extends Thread{
    int n = 0;
    @Override
    public void run() {
        Thread hello = new HelloTread();
        hello.start();
        try {
            hello.join();
        }catch (InterruptedException e){
            log.info("hello thread is interrupted");
        }
        hello.interrupt();
    }
}

@Slf4j
class HelloTread extends Thread{
    int n = 0;
    @Override
    public void run() {
        while (!isInterrupted()){
            n++;
            log.info("hello world {} times", n);
            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                log.info("HelloTread thread is interrupted");
            }
        }
    }
}